package controllers

import (
	// "crypto/md5"
	// "fmt"
	// // "crypto/rand"
	// // "encoding/base64"
	// "057dg/models"
	// "encoding/hex"
	// "encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	// "github.com/astaxie/beego/context"
	// "os"
	"strconv"
	// "strings"
	// "time"
	"057dg/models"
	"fmt"
)

type AccountCompanyController struct {
	beego.Controller
}

func (this *AccountCompanyController) Get() {
	this.Layout = "layout/accountCompany.html"
	// this.Data["IsLogin"] = checkAccount(this.Ctx)

	o := orm.NewOrm()
	var joblist []models.Job
	_, _ = o.QueryTable(new(models.Job)).All(&joblist)
	this.Data["joblist"] = joblist
	this.TplNames = "account/company_index.html"
}

func (this *AccountCompanyController) Company() {
	this.Layout = "layout/accountCompany.html"
	this.Data["IsLogin"] = checkAccount(this.Ctx)

	var o = orm.NewOrm()
	id, _ := this.GetInt("id")
	var company = &models.Company{Id: id}

	dosubmit := this.GetString("dosubmit")
	if dosubmit == "true" {
		input := this.Input()
		if o.Read(company) == nil {
			company.Name = input["name"][0]
			company.ShortName = input["shortName"][0]
			company.Tag = input["tag"][0]
			company.Logo = input["headImage"][0]
			company.Address = input["address"][0]
			company.Province = input["province"][0]
			company.City = input["city"][0]

			FinanceState, _ := this.GetInt("financeState")
			company.FinanceState = FinanceState

			company.WebsitUrl = input["websitUrl"][0]
			company.Weibo = input["weibo"][0]
			company.AndroidUrl = input["androidUrl"][0]
			company.Wechat = input["wechat"][0]
			company.ImgArray = input["imgArray"][0]
			company.Label = input["label"][0]
			Mfrate, _ := this.GetInt("mfrate")
			company.Mfrate = Mfrate
			company.IosUrl = input["iosUrl"][0]
			company.Description = input["description"][0]

			CompanySize, _ := this.GetInt("companySize")
			company.CompanySize = CompanySize

			if num, err := o.Update(company); err == nil {
				fmt.Println(num)
			}
			myjson := map[string]interface{}{}
			myjson["comapnyId"] = "1"
			myjson["companyPics"] = []interface{}{}
			myjson["STATE"] = "200"
			this.Data["json"] = myjson
			this.ServeJson()
		}
	}
	this.TplNames = "account/company.html"
}

func (this *AccountCompanyController) Issuejob() {
	this.Layout = "layout/layout.html"
	this.Data["Website"] = "beego.me"
	this.Data["Email"] = "astaxie@gmail.com"
	this.TplNames = "account/issuejob.html"

	o := orm.NewOrm()
	var job = new(models.Job)
	if id, _ := this.GetInt("id"); id != 0 {
		job = &models.Job{Id: id}
		o.Read(job)
	}
	this.Data["job"] = job
	if dosubmit := this.GetString("dosubmit"); dosubmit == "true" {
		fmt.Println(dosubmit)
		job.Name = this.GetString("name")
		job.Company = this.GetString("company")
		job.Address = this.GetString("address")
		job.Province = this.GetString("province")
		job.City = this.GetString("city")

		jobtype, _ := this.GetInt("jobType")
		job.JobType = jobtype

		job.Department = this.GetString("department")
		job.Team_intro = this.GetString("team_intro")
		job.Responsibility = this.GetString("responsibility")

		salary_max, _ := this.GetInt("salary_max")
		job.Salary_max = salary_max

		salary_min, _ := this.GetInt("salary_min")
		job.Salary_min = salary_min

		var uid int
		uid, _ = strconv.Atoi(this.Ctx.GetCookie("uid"))

		if id, _ := this.GetInt("id"); id != 0 {
			job.UpdateByUid = uid
			if num, err := o.Update(job); err == nil {
				fmt.Println(num)
			}
			fmt.Println("11")
		} else {
			job.CreateByUid = uid
			id, err := o.Insert(job)
			if err == nil {
				fmt.Println(id)
			}
			fmt.Println("12")
		}
	}
}

func (this *AccountCompanyController) EditPwd() {
	this.Layout = "layout/accountCompany.html"
	this.Data["isFlag"] = 0
	o := orm.NewOrm()
	uid, _ := strconv.Atoi(this.Ctx.GetCookie("uid"))
	user := &models.User{Id: uid}
	// fmt.Println(GetMd5String("SUCCESS198"))
	if dosubmit := this.GetString("dosubmit"); dosubmit == "true" {
		opwd := this.GetString("opwd")
		fmt.Println(opwd)
		fmt.Println(GetMd5String(opwd))
		fmt.Println(this.Ctx.GetCookie("password"))

		if GetMd5String(opwd) != this.Ctx.GetCookie("password") {
			fmt.Println("原密码不对")
			this.Redirect("/acompany/company", 301)
			return
		}
		npwd1 := this.GetString("npwd1")
		npwd2 := this.GetString("npwd2")
		if npwd1 != npwd2 {
			fmt.Println("两次输入不一致")
			this.Redirect("/acompany/company", 301)
			return
		}
		user.PassWord = GetMd5String(npwd1)
		if num, err := o.Update(user, "PassWord"); err == nil {
			fmt.Println(num)
			this.Redirect("/acompany/company", 301)
			return
		}
	}
	this.TplNames = "account/editpwd.html"
}
